package org.wlw.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class PropertiesUtils {

    private final static Map<String,Properties> maps = new HashMap<>();

    public static Properties getPropByFile(String filename) {
        //通过map来存储已经创建过的properties，这样不用每一次都新建对象
        if(maps.containsKey(filename)) {
            return maps.get(filename);
        }
        InputStream is = null;
        try {
            Properties prop = new Properties();
            is = PropertiesUtils.class.getClassLoader().getResourceAsStream(filename+".properties");
            prop.load(is);
            maps.put(filename,prop);
            return prop;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(is!=null) is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;

    }
}
